package com.swxc.smartorder.ui.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import com.lzy.okgo.model.HttpParams
import com.swxc.smartorder.R
import com.swxc.smartorder.app.GlobalAPI
import com.swxc.smartorder.entity.BackEntity
import com.swxc.smartorder.network.NetworkHelper
import com.swxc.smartorder.ui.base.BaseActivity
import com.swxc.smartorder.utils.JsonUtil
import kotlinx.android.synthetic.main.activity_feed_back.*

class FeedBackActivity : BaseActivity(), View.OnClickListener {
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                finish()
            }
            R.id.tv_submit -> {
                submitFeedBack()
            }
        }
    }

    var mHelper: NetworkHelper? = null
    private fun submitFeedBack() {
        val strContent = edt_feedback.text.toString().trim()
        if (strContent.isEmpty()) {
            showToastCommon("你的意见很宝贵！")

        } else {

            val httpParams = HttpParams()
            httpParams.put("content", strContent)
            httpParams.put("dataSource", "SERVICECENTER_STAFF_APP")
            mHelper?.postRequest(GlobalAPI.feedback, httpParams, object : NetworkHelper.NetWorkHelperBack {
                override fun requestSuccess(json: String) {

                    val fromJson = JsonUtil.fromJson<BackEntity>(json, BackEntity::class.java)
                            ?: return
                    when (fromJson.responseCode) {
                        1001 -> {
                            showToastSuccess("我们已收到你的反馈信息")
                            finish()
                        }
                        else -> {
                            showToastError("提交信息失败")
                        }
                    }

                    Log.e(BUG_TAG, json)
                }

                override fun requestFail() {

                }
            })
        }


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_feed_back)

        initViewAndEvent()
    }

    private fun initViewAndEvent() {
        iv_back.setOnClickListener(this)

        tv_submit.setOnClickListener(this)
        mHelper = NetworkHelper(mContext, supportFragmentManager)

    }
}
